//
//  String.swift
//  yzb
//
//  Created by cyptt on 2021/12/13.
//

import UIKit

extension String {


    public  func nsRange(range:Range<String.Index>) -> NSRange{
        let from = range.lowerBound.samePosition(in: utf16)
        let to = range.upperBound.samePosition(in: utf16)
        return NSRange(location: utf16.distance(from: utf16.startIndex, to: from!), length: utf16.distance(from: from!, to: to!))
    }
    
    /*
     获取第几位 0 位开始
     @loc 第几位
     */
    public func location(loc:Int) -> String{
        let index = self.index(self.startIndex, offsetBy: loc)
        let subCha = self[index]
        let subStr = String(subCha)
        return subStr
    }
    
    /*
     截取从开始后几位字符
     @num 开始后几位
     */
    public func startSubstring(num:Int)->String{
        let index = self.index(self.startIndex, offsetBy: num)
        let subCha = self[self.startIndex..<index]
        let subStr = String(subCha)
        return subStr
    }
    
    
    /*
     截取从结尾后几位字符
     @num 开始后几位
     */
    public func endSubstring(num:Int)->String{
        let index = self.index(self.endIndex, offsetBy: -num)
        let subCha = self[index..<self.endIndex]
        let subStr = String(subCha)
        return subStr
    }
    
    /*
     从第几位开始 首位为0
     @loc 开始的位置
     */
    public func subFrom(loc:Int)->String{
        let index = self.index(self.startIndex, offsetBy:loc)
        let subCha = self[index..<self.endIndex]
        let subStr = String(subCha)
        return subStr
    }
    
    /*
     到第几位结束 开始位0
     @loc 开始的位置
     */
    public func subTo(loc:Int)->String{
        let index = self.index(self.startIndex, offsetBy:loc)
        let subCha = self[self.startIndex...index]
        let subStr = String(subCha)
        return subStr
    }
    
    /*
     从第几位到第几位 开始位0
     @from 开始的位置
     @to   结束的位置 末尾位不包含 比如to=4 则是0123 四位
     */
    public func sub(from:Int,to:Int)->String{
        let fromindex = self.index(self.startIndex, offsetBy:from)
        let toindex = self.index(self.startIndex, offsetBy:to)
        let subCha = self[fromindex..<toindex]
        let subStr = String(subCha)
        return subStr
    }
    
    
    /*
     从哪个字符开始到哪个字符结束
     @fromChar 开始的字符
     @toChar   结束的字符
     */
    public func sub(fromChar:Character,toChar:Character)->String{
        guard let toindex = self.firstIndex(of: toChar),
              let fromindex = self.firstIndex(of: fromChar)
        else { return "" }
        
        let subCha = self[fromindex...toindex]
        let subStr = String(subCha)
        return subStr
    }
    
    
    /*
      从某一个字符开始
      开始位0
     @iscontain true 包含这个字符 false 不包含
     */
    public func sub(fromChar:Character,iscontain:Bool)->String{
        guard  let index = self.firstIndex(of: fromChar) else{
            return ""
        }
    
        var str = self[index..<self.endIndex]
        if iscontain == false{
            str.removeFirst()
        }
        let result = String(str)
       
        return result
    }
    
    
    
    /*
      截止到某个字符
     @iscontain true 包含这个字符 false 不包含
     */
    public func to(toChar:Character,iscontain:Bool)->String{
        guard  let index = self.firstIndex(of: toChar) else{
            return ""
        }
        var str = self[self.startIndex...index]
        if iscontain == false{
            str.removeLast()
        }
        let result = String(str)
       
        return result
    }
    
    /*
     字符串转字典
     @param arrSeparate 区分数组的
     @param comSeparate 区分字典的
     */
    public func toDic(arrSeparate:String,comSeparate:String)->[String:Any]{
        var resultDict:[String:Any] = [:]
        let arr:[String] = self.components(separatedBy:arrSeparate)
        for paramItem in arr{
            if paramItem != ""{
                let itemArr:[String] = paramItem.components(separatedBy: comSeparate)

             
                if itemArr.count == 2{

                    resultDict[itemArr[0]] =  itemArr[1]
                }
          
            }
        }
  
        return resultDict
    }
  
    
    /*
     脱敏
     @param from 开始位置
     @param to   结束位置
     */
    public func desensit(from:Int,to:Int,replacing:String)->String{
        
        let fromindex = self.index(self.startIndex, offsetBy:from)
        let toindex = self.index(self.startIndex, offsetBy:to)

        let rang:Range = fromindex..<toindex
        let sub:String? = self.replacingCharacters(in: rang, with: replacing)
       
        return sub ?? ""
    }
    
  
    
    /*
     从某个字符串开始
     @iscontain true 包含这个字符串 false 不包含
     */
    public func sub(fromString:String,iscontain:Bool)->String{
        
        if self.contains(fromString) == false{
            
            return ""
        }
        let arr:[String] = self.components(separatedBy: fromString)
   
        
        if arr.count >= 1{
            if iscontain == true{
                return fromString + arr[1]
            }else{
                return arr[1]

            }
          
        }else{
            return ""
        }
    }
    
    /*
     截取到某个字符串
     @iscontain true 包含这个字符串 false 不包含
     */
    public func sub(toString:String,iscontain:Bool)->String{
        
        if self.contains(toString) == false{
            
            return ""
        }
        let arr:[String] = self.components(separatedBy: toString)
       
        
        if arr.count >= 1{
            if iscontain == true{
                return arr[0] + toString
            }else{
                return arr[0]

            }
          
        }else{
            return ""
        }
    }
    
}

